//选中列表的所有行并更改行样式
$(function() {
    $('#select_all').bind("click", function () {
        if ( this.checked == true ) {
            $("input[class='select-box']").each( function() {
                this.checked = true;
                this.parentNode.parentNode.className = 'checked';
            });
        } else {
            $("input[class='select-box']").each( function() {
                this.checked = false;
                this.parentNode.parentNode.className = ''; 
            });
        }
    });
});

//擦除选中用户列表行
$(function() {
    $('#user_delete_btn').bind("click", function() {
        
        var value = new Array();
        $("input[class='select-box']").each( function() {
           if ( this.checked == true ) {
               value.push( this.value); 
           } 
        });
        
        if ( value.length > 0 ) {
            var warning = confirm( "确定删除选中用户？");
        } else {
            return false;
        }
            
        if( warning == true ) {
            value = value.join(",");
            var btn = $('#user-delete-btn').button('loading');
            $.post( url('admin/userdelete'), { 'value':value }, function(data) {
                btn.button('reset');
                if ( data.delresult == true ) {
                    location = url( 'admin/userlist');
                } else if ( data.delresult == false ) {
                    $('body').append("<div class='alert alert-warning' style='position:absolute; padding: 50px;opacity: 1;'>数据读写异常，请稍后再试！<button type='button' class='close' data-dismiss='alert' style='padding: 0px 15px;position: absolut; right: 20px;top: 90px;border: 1px solid #E2DDDD;background-color: #EFEAEA; opacity: 1;color: #8A6D3B;'>&times;</button></div>");
                    var left = ($( window ).width() - $( ".alert " ).width()) / 2;
                    var top = ($( window ).height() - $( ".alert " ).height()) / 2;
                    $('.alert').css({left:left, top: top });
                }
            }, 'json');

        } else {
            return false;
        }
    })
});

//点击列表行时更改该行的checked属性和行样式
$(function() {
    $('tbody > tr').each( function() {
        this.onclick = function() {
            console.log(this.firstChild);
            var input = this.firstChild.firstChild;
            if ( input.checked == false ) {
                input.checked = true;
                this.className = 'checked';
            } else {
                input.checked = false;
                this.className = ''; 
            }
        };
    });
});

//点击列表行的input：select-box时阻止冒泡和更改改行样式
$(function() {
    $(".select-box").bind('click', function(event){
        event.stopPropagation(); 
        if( this.checked == true ) {
            this.parentNode.parentNode.className = 'checked';
        } else {
            this.parentNode.parentNode.className = '';
        }
    }); 
});

//阻止列表行链接点击时冒泡
$(function() {
    $(".line-btn").bind('click', function(event){
        event.stopPropagation();
    }); 
});

//分类列表选中项按钮点击函数
$(function() {
    $("#handle_artsort").bind('click', function() {
        if ( $(".artsort-act-border").css('display') == 'block' ) {
            $(".artsort-act-border").css('display', 'none');
        } else {
            $(".artsort-act-border").css('display', 'block');
        }
    });
});

//input时间插件启动函数
$('.datepicker').datepicker();
